package com.yu1996.gulimall.product.app;

import com.yu1996.gulimall.product.entity.ProductAttrValueEntity;
import com.yu1996.gulimall.product.service.ProductAttrValueService;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yu1996.gulimall.product.vo.AttrRespVo;
import com.yu1996.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yu1996.gulimall.product.service.AttrService;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.R;

/**
 * 商品属性
 *
 * @author yu
 * @email 1798121567@qq.com
 * @date 2021-08-04 17:31:00
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
  @Autowired private AttrService attrService;
  @Autowired private ProductAttrValueService productAttrValueService;

  /** 修改商品spu规格参数列表 */
  @PostMapping("/update/{spuId}")
  public R updateSpuAttr(
      @PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities) {
    productAttrValueService.updateSpuAttr(spuId, entities);
    return R.ok();
  }

  /** 查询商品spu规格参数列表 */
  @GetMapping("/base/listforspu/{spuId}")
  public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
    List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);
    return R.ok().put("data", entities);
  }

  /** 根据key模糊查询分页显示列表 */
  @GetMapping("/{attrType}/list/{catelogId}")
  public R baseAttrList(
      @RequestParam Map<String, Object> params,
      @PathVariable("catelogId") Long catelogId,
      @PathVariable("attrType") String type) {

    PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);

    return R.ok().put("page", page);
  }

  /** 列表 */
  @RequestMapping("/list")
  // @RequiresPermissions("product:attr:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = attrService.queryPage(params);

    return R.ok().put("page", page);
  }

  /** 信息 */
  @RequestMapping("/info/{attrId}")
  public R info(@PathVariable("attrId") Long attrId) {
    //		AttrEntity attr = attrService.getById(attrId);
    AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
    return R.ok().put("attr", attrRespVo);
  }

  /** 保存 */
  @RequestMapping("/save")
  // @RequiresPermissions("product:attr:save")
  public R save(@RequestBody AttrVo attr) {
    attrService.saveAttr(attr);

    return R.ok();
  }

  /** 修改 */
  @RequestMapping("/update")
  // @RequiresPermissions("product:attr:update")
  public R update(@RequestBody AttrRespVo attr) {
    attrService.updateAttr(attr);

    return R.ok();
  }

  /** 删除 */
  @RequestMapping("/delete")
  // @RequiresPermissions("product:attr:delete")
  public R delete(@RequestBody Long[] attrIds) {
    attrService.removeByIds(Arrays.asList(attrIds));

    return R.ok();
  }
}
